package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.DailyProfit;
import com.pets.springboot_petshop.entity.MonthlyProfit;
import com.pets.springboot_petshop.entity.Order;
import com.pets.springboot_petshop.entity.Useraddress;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/15 下午1:30 周二
 * @Description: 数据访问层接口
 */
@Mapper
@Repository
public interface OrderMapper {
    //id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "b.email, " +
            "c.name, " +
            "a.message, " +
            "d.name as membershiptype_name, " +
            "a.tk_status,"+
            "c.phone, " +
            "c.address_line1, " +
            "c.address_line2, " +
            "a.payment_method, " +
            "a.ordernumber ," +
            "a.create_time ," +
            "a.discount,"+
            "a.update_time " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN membershiptype d ON b.membership_id = d.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id " +
            "WHERE a.id = #{id}")
    Order select(int id);
    //ordernumber查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "a.tk_status, " +
            "c.name, " +
            "a.tk_status,"+
            "c.phone, " +
            "c.address_line1, " +
            "c.address_line2, " +
            "a.payment_method, " +
            "a.ordernumber ," +
            "a.create_time ," +
            "a.discount,"+
            "a.update_time " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id " +
            "WHERE a.ordernumber like concat('%',#{ordernumber},'%');")
    List<Order> selectordernumber(int ordernumber);
    //日期查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "a.tk_status, " +
            "c.name, " +
            "c.phone, " +
            "c.address_line1, " +
            "c.address_line2, " +
            "a.payment_method, " +
            "a.ordernumber, " +
            "a.create_time, " +
            "a.update_time, " +
            "a.discount " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id " +
            "WHERE DATE(a.create_time) = #{date}")
    List<Order> selectOrderByDate(LocalDate date);
    //年销售
    @Select("WITH RECURSIVE MonthSequence AS (" +
            "  SELECT 1 AS month" +
            "  UNION ALL" +
            "  SELECT month + 1 " +
            "  FROM MonthSequence " +
            "  WHERE month < 12" +
            ")" +
            "SELECT " +
            "  #{year} as year, " +
            "  ms.month, " +
            "  SUM(CASE WHEN a.status = '已完成' THEN a.total_amount ELSE 0 END) as totalAmount " +
            "FROM MonthSequence ms " +
            "LEFT JOIN `order` a ON ms.month = MONTH(a.create_time) " +
            "AND YEAR(a.create_time) = #{year} " +
            "AND a.status = '已完成' " +
            "GROUP BY ms.month " +
            "ORDER BY ms.month")
    List<MonthlyProfit> selectMonthlyProfit(@Param("year") int year);
    //月销售
    @Select("WITH RECURSIVE DateSequence AS (" +
            "  SELECT 1 AS day" +
            "  UNION ALL" +
            "  SELECT day + 1 " +
            "  FROM DateSequence " +
            "  WHERE day < 31" +
            ")" +
            "SELECT " +
            "  #{year} as year, " +
            "  #{month} as month, " +
            "  ds.day, " +
            "  SUM(CASE WHEN a.status = '已完成' THEN a.total_amount ELSE 0 END) as totalAmount " +
            "FROM DateSequence ds " +
            "LEFT JOIN `order` a ON ds.day = DAY(a.create_time) " +
            "AND YEAR(a.create_time) = #{year} " +
            "AND MONTH(a.create_time) = #{month} " +
            "AND a.status = '已完成' " +
            "GROUP BY ds.day " +
            "ORDER BY ds.day")
    List<DailyProfit> selectDailyProfit(@Param("year") int year, @Param("month") int month);
    //每个月的退货率
    @Select("SELECT " +
            "YEAR(create_time) as year, " +
            "MONTH(create_time) as month, " +
            "SUM(CASE WHEN a.status = '已退款' THEN 1 ELSE 0 END) as refundCount, " +
            "SUM(CASE WHEN a.status IN ('已完成', '已退款') THEN 1 ELSE 0 END) as totalOrderCount " +
            "FROM `order` a " +
            "WHERE YEAR(create_time) = #{year} " +
            "AND a.status IN ('已完成', '已退款') " +
            "GROUP BY YEAR(create_time), MONTH(create_time)")
    List<MonthlyProfit> selectMonthlyRefundRate(@Param("year") int year);
    //年度的退货率
    @Select("SELECT " +
            "YEAR(create_time) as year, " +
            "SUM(CASE WHEN a.status = '已退款' THEN 1 ELSE 0 END) as refundCount, " +
            "SUM(CASE WHEN a.status IN ('已完成', '已退款') THEN 1 ELSE 0 END) as totalOrderCount " +
            "FROM `order` a " +
            "WHERE YEAR(create_time) = #{year} " +
            "AND a.status IN ('已完成', '已退款') " +
            "GROUP BY YEAR(create_time)")
    List<MonthlyProfit> selectAnnualRefundRate(@Param("year") int year);
    //user_id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "c.name, " +
            "a.tk_status,"+
            "c.phone, " +
            "c.address_line1, " +
            "c.address_line2, " +
            "a.payment_method, " +
            "a.ordernumber ," +
            "a.discount,"+
            "a.create_time ," +
            "a.update_time " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id " +
            "WHERE a.user_id = #{user_id}")
    List<Order> selectuser_id(int user_id);
    //user_id查询
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "a.tk_status,"+
            "c.name, " +
            "c.phone, " +
            "c.address_line1, " +
            "a.ordernumber, " +
            "c.address_line2, " +
            "a.payment_method, " +
            "a.create_time ," +
            "a.discount,"+
            "a.update_time " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id " +
            "WHERE a.status = #{status}")
    List<Order> selectstatus(String status);
    //查询所有
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "b.username, " +
            "a.useraddress_id, " +
            "a.total_amount, " +
            "a.status, " +
            "a.tk_status,"+
            "c.phone, " +
            "c.name, " +
            "c.address_line1, " +
            "c.address_line2, " +
            "a.ordernumber, " +
            "a.payment_method, " +
            "a.create_time ," +
            "a.discount,"+
            "a.update_time " +
            "FROM `order` a " +
            "JOIN user b ON a.user_id = b.id " +
            "JOIN useraddress c ON a.useraddress_id = c.id ")
    List<Order> findAll();
    //添加
    @Insert("insert into `order`(user_id,useraddress_id,total_amount,discount,status,payment_method,ordernumber,message,create_time,update_time)" +
            "values(#{user_id},#{useraddress_id},#{total_amount},#{discount},#{status},#{payment_method},#{ordernumber},#{message},now(),now());")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Order order);
    //删除
    @Delete("delete from `order` where id=#{id};")
    int delete(int id);
    //修改数据
    @Update("UPDATE `order` SET user_id=#{user_id},useraddress_id=#{useraddress_id},total_amount=#{total_amount},ordernumber=#{ordernumber}," +
            "status=#{status},tk_status=#{tk_status},payment_method=#{payment_method},update_time=now() where id= #{id};")
    int update(Order order);
    //gly修改数据
    @Update("UPDATE `order` SET total_amount=#{total_amount},status=#{status},payment_method=#{payment_method},update_time=now() where id= #{id};")
    int adminupdate(Order order);
    //修改状态
    @Update("UPDATE `order` SET status=#{status},update_time=now() where id= #{id};")
    int updatestatus(Order order);
    //修改状态
    @Update("UPDATE `order` SET tk_status=#{tk_status},update_time=now() where id= #{id};")
    int updatetk_status(Order order);
    // 批量修改status
    @Update({
            "<script>",
            "UPDATE `order` SET status=#{status}, update_time=now() WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateStatus(@Param("array") int[] ids, @Param("status") String status);
    // 批量删除
    @Delete({
            "<script>",
            "DELETE FROM `order` WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int admindelete(@Param("array") int[] ids);
}
